package xyz.msj.hrm.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.msj.hrm.doc.CourseDocument;
import xyz.msj.hrm.query.CourseQuery;
import xyz.msj.hrm.repository.CourseElasticsearchRepository;
import xyz.msj.hrm.service.ICourseEelasticService;
import xyz.msj.hrm.util.AjaxResult;
import xyz.msj.hrm.util.PageList;

/**
 * es全文检索
 * //针对课程的ES操作
 */
@RestController
public class ESController {
    @Autowired
    private ICourseEelasticService courseService;
    //es库
    @Autowired
    private CourseElasticsearchRepository courseElasticsearchRepository;
    /**
     * 保存数据到es
     * @return
     */
    @RequestMapping("/es/save")
    public AjaxResult save(@RequestBody CourseDocument courseDocument){
        try {
            //保存到es
            courseElasticsearchRepository.save(courseDocument);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败");
        }
    }

    /**
     * 删除es数据
     *
     * @return
     */
    @RequestMapping("/es/del")
    public AjaxResult delete(@RequestBody CourseDocument courseDocument) {
        try {
            //删除到es
            courseElasticsearchRepository.delete(courseDocument);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

    /**
     * 根据条件查询课程对象
     * @param courseQuery
     * @return
     */
    @RequestMapping("/es/search")
    public AjaxResult search(@RequestBody CourseQuery courseQuery){
        try {
            PageList<CourseDocument> result = courseService.search(courseQuery);
            return AjaxResult.me().setResultObj(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败！！");
        }
    }


}
